package com.kuang.lock;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: juc
 * @description: synchronized锁
 * @author: 陈恩涛
 * @create: 2021-03-29 09:53
 **/
public class Demo01 {
    public static void main(String[] args) {
        Phone phone = new Phone();

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                Runtime.getRuntime().availableProcessors(),
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy()
        );

        threadPoolExecutor.execute(() -> {
            phone.sendMsg();
        });

        threadPoolExecutor.execute(() -> {
            phone.sendMsg();
        });
    }
}

class Phone {
    public synchronized void sendMsg() {
        System.out.println(Thread.currentThread().getName()+" 发短信！");
        call(); // 这里也有锁
    }

    public synchronized void call() {
        System.out.println(Thread.currentThread().getName()+" 打电话！");

    }

}
